package in.muton.edushare.poll.questions;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.PollOptions;
import com.edushare.common.vo.PollQuestions;

public class CheckBoxQuestion extends QuestionBase {

	public CheckBoxQuestion(String id, IModel<PollQuestions> model,
			EduShareBasePage page, final boolean preview, final boolean editMode) {
		super(id, model, page, preview, editMode);
		List<PollOptions> poList = getQuestionModel().getObject().getOptions();
		Collections.sort(poList);
		retrieveValues();
		CayenneList<PollOptions> po = new CayenneList<PollOptions>(poList);
		add(new ListView<IModel<PollOptions>>("optionList", po) {
			private static final long serialVersionUID = 286372665924118868L;

			@Override
			protected void populateItem(ListItem<IModel<PollOptions>> arg0) {
				PollOptions p = arg0.getModelObject().getObject();
				final Integer opt = new Integer(p.getOptionId());
				arg0.add(new Label("optionName", p.getOptionString()));
				arg0.add(new CheckBox("option", new Model<Boolean>() {
					@Override
					public Boolean getObject() {
						return selection.get(opt);
					}

					@Override
					public void setObject(Boolean b) {
						selection.put(opt, b);
					}

					private static final long serialVersionUID = 874631162243197710L;
				}));
			}
		});
	}

	private HashMap<Integer, Boolean> selection = new HashMap<Integer, Boolean>();
	private static final long serialVersionUID = -821716705533192473L;

	@Override
	public boolean retrieveValues() {
		boolean retVal = true;
		if (isPreview())
			retVal = false;
		UserPollDAO upDAO = new UserPollDAO(getORMInterface());
		List<PollOptions> poList = upDAO.getAnswer(getLoggedInUser(),
				getQuestionModel().getObject());
		if (poList.isEmpty())
			retVal = false;
		List<PollOptions> fullList = getQuestionModel().getObject()
				.getOptions();
		for (PollOptions po : fullList) {
			if (poList.contains(po)) {
				selection.put(po.getOptionId(), true);
			} else
				selection.put(po.getOptionId(), false);
		}
		return retVal;
	}

	@Override
	public boolean save(boolean draft) {
		if ((!draft &&  !verify()) || isEditMode()||isPreview())
			return false;
		UserPollDAO uPDao = new UserPollDAO(getORMInterface());
		Integer[] arr = new Integer[selection.size()];
		Integer[] keys = new Integer[selection.size()];
		keys = selection.keySet().toArray(keys);
		int j = 0;
		for (Integer i : keys) {
			if (selection.get(i)) {
				arr[j++] = i;
			}
		}
		uPDao.setAnswer(getLoggedInUser(), getQuestionModel().getObject(), arr);
		return true;
	}

	@Override
	public boolean verify() {
		return true;
	}

	@Override
	public void clear() {
		new UserPollDAO(getORMInterface()).clearAnswers(getLoggedInUser(),
				getQuestionModel().getObject());
		Set<Integer> keys = selection.keySet();
		for(Integer key:keys){
			selection.put(key, false);
		}
	}
}
